/**
 * author:Administrator
 * date:2016/1/4
 * desc: koa-better-body in router
 */

'use strict';

var bodyParser = require('koa-better-body');
var koaBody = bodyParser({});
var config = require('../config/index.js');
var Router = require('koa-router');
var __pathPrefix = config.urlPrefix + '/users';
console.log('router %s' + __pathPrefix);
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();
var service = require('../service/user.js');

/**
 * post /users
 {
 "name":"wzq",
 "email":"foo@bar.com",
 "password":"abc123",
 "type":"client"
 }
 client - merchant
 */
router.post('/', koaBody, function*() {
  var json = this.request.body.fields;
  console.log(json);
  var retObj = yield service.register(json);
  this.body = retObj;
});

router.post('/login', koaBody, function*() {
  var json = this.request.body.fields;
  console.log(json);
  var retObj = yield service.login(json);
  this.body = retObj;
});